//: C20:ListSpecialFunctions.cpp

#include <list>
#include <iostream>
#include <iterator>
#include <algorithm>

#include "Noisy.h"

using namespace std;

ostream_iterator<Noisy> out(cout, " ");

void print(list<Noisy>& ln, char* comment = "") {
    cout << "\n" << comment << ":\n";
    copy(ln.begin(), ln.end(), out);
    cout << endl;
}

int main() {
    typedef list<Noisy> LN;

    LN l1;
    LN l2;
    LN l3;
    LN l4;

    generate_n(back_inserter(l1), 6, NoisyGen());
    generate_n(back_inserter(l2), 6, NoisyGen());
    generate_n(back_inserter(l3), 6, NoisyGen());
    generate_n(back_inserter(l4), 6, NoisyGen());

    print(l1, "l1");
    print(l2, "l2");
    print(l3, "l3");
    print(l4, "l4");

    LN::iterator it1 = l1.begin();

    it1++;
    it1++;
    it1++;

    l1.splice(it1, l2);

    print(l1, "l1 after splice(it1, l2)");
    print(l2, "l2 after splice(it1, l2)");

    LN::iterator it2 = l3.begin();

    it2++;
    it2++;
    it2++;

    l1.splice(it1, l3, it2);

    print(l1, "l1 after splice(it1, l3, it2)");

    LN::iterator it3 = l4.begin();
    LN::iterator it4 = l4.end();

    it3++;
    it4++;

    l1.splice(it1, l4, it3, it4);
    print(l1, "l1 after splice(it1, l4, it3, it4)");

    Noisy n;
    LN l5(3, n);

    generate_n(back_inserter(l5), 4, NoisyGen());
    l5.push_back(n);
    print(l5, "l5 before remove()");

    l5.remove(l5.front());
    print(l5, "l5 after remote()");

    l1.sort();
    l5.sort();
    l5.merge(l1);

    print(l5, "l5 after l5.merge(l1)");

    cout << "\n Cleanup" << endl;

    return 0;
} ///:~

